package com.dg11185.nearshop.view.adapter;

import java.util.List;

import com.android.nearshop.R;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.nearshop.app.DefaultFactory;
import com.dg11185.nearshop.net.entity.ActivityRecord;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author：Daibo    
 * @time：2014-9-2 上午9:28:33
 */
public class LotteryRecordAdapter extends BaseAdapter {

    private static final String URL_STRING = "http://f4.lashouimg.com/cms/M00/5F/7F/CqgBHlP_2IiAM5xeAAEPSj7_LLw324-440x280.jpg";
    private LayoutInflater inflater = null;

    private List<ActivityRecord> records = null;

    public LotteryRecordAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setRecords(List<ActivityRecord> records) {
        this.records = records;
    }

    @Override
    public int getCount() {
        if (records == null) {
            return 0;
        } else {
            return records.size();
        }
    }

    @Override
    public Object getItem(int arg0) {
        return records.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ActivityRecord record = records.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.user_lottery_item, parent, false);
            holder.image = (ImageView)convertView.findViewById(R.id.lottery_record_img);
            holder.title = (TextView)convertView.findViewById(R.id.lottery_record_title);
            holder.time = (TextView)convertView.findViewById(R.id.lottery_record_time);
            holder.state = (TextView)convertView.findViewById(R.id.lottery_record_state);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(URL_STRING, holder.image, DefaultFactory.getImageoptions());
        holder.title.setText(record.activityName);
        holder.time.setText(record.createTime);
        holder.state.setText(record.joinResult);
        return convertView;
    }

    private class ViewHolder {
        private TextView title;

        private ImageView image;

        private TextView time;

        private TextView state;
    }
}
